// pages/myFriend/myFriend.js
const db=wx.cloud.database()
const defaultList=db.collection('defaultList')
const concern_fans=db.collection('concern_fans')
const specialConcern=db.collection('specialConcern')
const _=db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    defaultList:[],
    specialConcern:[],
    current:'',
    index:'',
    concern_fans:[],
    isShow:'true',
    specialisShow:'true',
    defaultCount:'',
    specialCount:'',
    gzcount:''
  },
  // 列表显示与否
  openClose2(e){
    // console.log(e,'点击率');
    // console.log(this.data.isShow);
    const isShow=this.data.isShow
    if(isShow==true){
      this.setData({
        isShow:false,
      })
    }else {
      this.setData({
        isShow:true,
      })
    } 
  },
  openClose1(e){
    const specialisShow=this.data.specialisShow
    if(specialisShow==true){
      this.setData({
        specialisShow:false,
        
      })
    }else {
      this.setData({
        specialisShow:true,    
      })
    } 
  },
  // 获取要关注的up主的信息
  getInfo(e){
    wx.setStorageSync('info',e.detail)
  },
  specialConcern(){
    let info=wx.getStorageSync('info')
    specialConcern
    .add({
      data:{
        up:info.up,
        digest:info.digest,
        avatar:info.avatar
      }
    })
  },
  // 默认组的关注
  defaultConcern(){
    let info=wx.getStorageSync('info')
    defaultList
    .add({
      data:{
        up:info.up,
        digest:info.digest,
        avatar:info.avatar
      }
    })
  },
  specialCancle(event){
    let tapIndex=event.detail.tapIndex
    let _id=event.detail._id
    if(tapIndex==1){
        wx.cloud.callFunction({
          name:'remove',
          data:{
            _id:_id
          }
        })
    }
    else if(tapIndex==0){
      let info=wx.getStorageSync('info')
      defaultList
      .add({
        data:{
          up:info.up,
          digest:info.digest,
          avatar:info.avatar
        }
      })
      wx.cloud.callFunction({
        name:'remove',
        data:{
          _id:_id
        }
      }) 
    }
  },
  // 默认组的取消关注
  defaultCancle(event){   
    let tapIndex=event.detail.tapIndex
    let _id=event.detail._id
    // console.log(tapIndex);
    // console.log(_id); 
    if(tapIndex==1){
        wx.cloud.callFunction({
          name:'remove',
          data:{
            _id:_id
          }
        })
    }
    else if(tapIndex==0){
      let info=wx.getStorageSync('info')
      specialConcern
      .add({
        data:{
          up:info.up,
          digest:info.digest,
          avatar:info.avatar
        }
      })
      wx.cloud.callFunction({
        name:'remove',
        data:{
          _id:_id
        }
      }) 
    }
  },
  currentPage(e){
    this.setData({
      current:e.detail.current
      })
  },
  currentNav(e){
    this.setData({
      index:e.currentTarget.dataset.index,
      current:e.currentTarget.dataset.index
    })
    },


  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let dL=await defaultList.get()
    let cf=await concern_fans.get()
    let sc=await specialConcern.get()
    this.setData({
      defaultList:dL.data,
      concern_fans:cf.data, 
      specialConcern:sc.data  
    }) 
    // 获取关注数
    const spcount=this.data.specialConcern.length
    const dfcount=this.data.defaultList.length
    this.setData({
      defaultCount:dfcount,
      specialCount:spcount
    })
    wx.setStorageSync('gzcount', spcount+dfcount)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})